<template>
  <view class="meeting">
    <Tabs :options="options" v-model="tab" class="tab" @change="onTabsChange" v-if="!isMyBacklog" />
    <template v-if="Array.isArray(list) && list.length">
      <MeetingItem
        class="meetign-item"
        v-for="(item, index) in list"
        :key="item.meetingId"
        :data="item"
        :index="index"
        :type="tab"
      />
    </template>
    <CuEmpty v-else />
  </view>
</template>

<script>
import MeetingItem from './components/MeetingItem.vue'
import Tabs from './components/Tabs.vue'
import { getList, getListForFinish } from '@/api/duty-basic/duty-meeting'
import CuEmpty from '@/components/cu-empty/index.vue'
export default {
  name: '',
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      options: [
        { value: 1, label: '当前会议' },
        { value: 2, label: '已完成会议' }
      ],
      tab: 1,
      list: [],
      total: 0,
      isMyBacklog: false
    }
  },
  components: { Tabs, MeetingItem, CuEmpty },
  methods: {
    async getList(isRefresh = true) {
      const request = this.tab === 1 ? getList : getListForFinish
      const { data: { rows = [], total } = {} } = await request({
        ...this.queryParams,
        isMyBacklog: this.isMyBacklog ? 'Y' : undefined
      }).finally(() => uni.stopPullDownRefresh())
      if (isRefresh) {
        this.list = rows
      } else {
        this.list = [...this.list, ...rows]
      }
      this.total = total
    },
    onTabsChange() {
      this.list = []
      this.queryParams.pageNum = 1
      this.getList()
    }
  },
  onShow() {
    this.getList()
  },
  onNavigationBarButtonTap() {
    uni.navigateTo({ url: '/pages/work/online-examination/record' })
  },
  onPullDownRefresh() {
    this.queryParams.pageNum = 1
    this.getList()
  },
  onReachBottom() {
    const { pageNum, pageSize } = this.queryParams
    if (pageNum * pageSize < this.total) {
      this.queryParams.pageNum += 1
      this.getList(false)
    }
  },
  onLoad({ isMyBacklog } = {}) {
    this.isMyBacklog = isMyBacklog === '1'
    if (this.isMyBacklog) uni.setNavigationBarTitle({ title: '当前会议待办' })
  }
}
</script>

<style lang="scss" scoped>
.meetign-item {
  margin-top: 20rpx;
  margin-bottom: 30rpx;
}
</style>
